package week03;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Description： 全排列
 * 地址：
 * Author: liujiao
 * Date: Created in 2022/1/18 17:52
 * email: liujiao@fcbox.com
 * Version: 0.0.1
 */
public class AllSortSolution {

    public static void main(String[] args) {
        int[] nums = new int[]{1,2,3};
        List<List<Integer>> lists = permuteUnique(nums);
        System.out.println(lists);
    }

    public static List<List<Integer>> permuteUnique(int[] nums) {
        Arrays.sort(nums);
        List<List<Integer>> lists = new ArrayList<>();
        dfsort(lists, new ArrayList<>(), nums, 0);
        return lists;
    }

    private static void dfsort(List<List<Integer>> lists, List<Integer> list, int[] nums, int index) {
        if (index == nums.length) {
            lists.add(new ArrayList<>(list));
            return;
        }
        for (int i = 0; i < nums.length; i++) {
            if ((i > 0 && nums[i] == nums[i - 1]) || nums[i] == 100) {
                continue;
            } else {
                int temp = nums[i];
                list.add(temp);
                nums[i] = 100;
                dfsort(lists, list, nums, index + 1);
                nums[i] = temp;
                list.remove(list.size() - 1);
            }
        }
    }
}
